/*
 * Screen.c
 *
 *  Created on: May 23, 2021
 *      Author: user
 */

#include "Screen.h"

#include "stm32f4xx_ll_usart.h"

#define ButtonIncrease_Addr 0x000A
#define ButtonDecrease_Addr 0x0009

static uint8_t USART3_RxBuff;
#define DATA_LENGTH 7
static uint8_t USART3_ReceivedData[7];
static uint8_t USART3_ReceivedDataDealedFlag = 1; // 上次接受到的数据是否已被处理过

static void TransmitData8(USART_TypeDef *USARTx, uint8_t Value)
{
	while (LL_USART_IsActiveFlag_TC(USARTx) != 1) // 等待上次发送结束
	{
	}
	LL_USART_TransmitData8(USARTx, Value);
}

void displayNum(uint16_t addr, float data)
{
	//next get data high & low bits
	int16_t temp;
	temp = data * 10;
	uint8_t high8, low8;
	high8 = (temp & 0xff00) >> 8;
	low8 = temp & 0x00ff;
	//next get address high & low bits
	uint8_t addr_high, addr_low;
	addr_high = (addr & 0xff00) >> 8;
	addr_low = (addr & 0x00ff);
	//send messages
	TransmitData8(USART3, (uint8_t)0xA5);
	TransmitData8(USART3, (uint8_t)0x5A);
	TransmitData8(USART3, (uint8_t)0x05);
	TransmitData8(USART3, (uint8_t)0x82);
	TransmitData8(USART3, (uint8_t)addr_high);
	TransmitData8(USART3, (uint8_t)addr_low);
	TransmitData8(USART3, (uint8_t)high8);
	TransmitData8(USART3, (uint8_t)low8);
}

void displayText(uint16_t addr, const char* text, uint8_t text_length)
{
	//next get address high & low bits
	uint8_t addr_high, addr_low;
	addr_high = (addr & 0xff00) >> 8;
	addr_low = (addr & 0x00ff);
	//send messages
	TransmitData8(USART3, 0xA5);
	TransmitData8(USART3, 0x5A);
	TransmitData8(USART3, text_length+5);
	TransmitData8(USART3, 0x82);
	TransmitData8(USART3, addr_high);
	TransmitData8(USART3, addr_low);
	for (int i=0;i<text_length;i++)
	{
		TransmitData8(USART3, text[i]);
	}
	TransmitData8(USART3, 0xff);
	TransmitData8(USART3, 0xff);
}

static uint8_t Head1_ReceivedFlag = 0;
static uint8_t Head2_ReceivedFlag = 0;
static uint8_t received_length = 0;
void ReceiveDataITService(void)
{
	USART3_RxBuff = LL_USART_ReceiveData8(USART3);
	if((!Head1_ReceivedFlag) && USART3_RxBuff == 0xa5 )
	{
		Head1_ReceivedFlag = 1;
	}
	else if((!Head2_ReceivedFlag) && Head1_ReceivedFlag)
	{
		if(USART3_RxBuff == 0x5a)
		{
			Head2_ReceivedFlag = 1;
			received_length = 0;
		}
		else
		{
			Head1_ReceivedFlag = 0;
			Head2_ReceivedFlag = 0;
			received_length = 0;
		}
	}
	else if(Head1_ReceivedFlag && Head2_ReceivedFlag && received_length < DATA_LENGTH)
	{
		USART3_ReceivedData[received_length] = USART3_RxBuff;
		received_length ++;
	}
	else if(received_length >= DATA_LENGTH)
	{
		Head1_ReceivedFlag = 0;
		Head2_ReceivedFlag = 0;
		received_length = 0;
		USART3_ReceivedDataDealedFlag = 0;
	}
}

ButtonType recieveButtonPushed(void)
{
	if(USART3_ReceivedDataDealedFlag)
	{
		return NoneButtonPushed;
	}
	uint16_t ReceivedButtonAddr = ((USART3_ReceivedData[2] & 0x00ff) << 8) + USART3_ReceivedData[3];
	ButtonType ButtonPushed = NoneButtonPushed;
	switch(ReceivedButtonAddr)
	{
	case ButtonDecrease_Addr:
		ButtonPushed = ButtonDecrease;
		break;
	case ButtonIncrease_Addr:
		ButtonPushed = ButtonIncrease;
		break;
	}
	USART3_ReceivedDataDealedFlag = 1;
	return ButtonPushed;
}
